import { Component, OnInit } from '@angular/core';
import { Http } from "@angular/http";
import { DataTableModule, SharedModule } from 'primeng/primeng';
import { ApplicationService } from "../../../service/application.service";
import { Farm } from "../../../entity/farm.model";
import { BladeData } from "../../../shared/blade/blade-data";
import { DynamicReportService } from "../../../service/dynamic-report.service";

@Component({
  selector: 'app-farm-indicator',
  templateUrl: './farm-indicator.component.html',
  styleUrls: ['./farm-indicator.component.css']
})
export class FarmIndicatorComponent implements OnInit {
  farms: Farm[] = [];

  //当前风场
  currentFarm: Farm;



  //当前风场集合
  currentFarms: Farm[] = [];
  //当前风场Id集合
  currentFarmIds: number[] = [];



  //指标集合
  targets: any[] = [
    { name: '发电量(kWh)', key: 'ActivePower', value: 1, isChecked: true },
    { name: '平均风速(m/s)', key: 'WindSpeed', value: 2, isChecked: true }
  ];
  //结束时间
  endtime = new Date();
  //开始时间
  starttime: Date = new Date(this.endtime.getFullYear(), this.endtime.getMonth(), this.endtime.getDate() - 1)
  //点击的指标集合
  points: string[] = ['ActivePower', 'WindSpeed'];
  //风场级指标集合
  farmIndicators: any[] = [];
  selectedFarmIndicators: any[] = [];
  //错误信息
  message: string;
  //是否显示弹出层
  isShow: boolean = false;
  /**
   *日期格式化
   *
   * @type {*}
   * @memberof FarmIndicatorComponent
   */
  en: any;
  constructor(
    private http: Http,
    public application: ApplicationService,
    private dynamicReportService: DynamicReportService,
  ) { }

  ngOnInit() {
    this.en = {
      firstDayOfWeek: 0,
      dayNames: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
      dayNamesShort: ['日', '一', '二', '三', '四', '五', '六'],
      dayNamesMin: ['日', '一', '二', '三', '四', '五', '六'],
      monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
      monthNamesShort: ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'],
      today: '今天',
      clear: '清除'
    };
    this.farms = this.application.currentUserFarms.filter(farm => farm.farmCategory == "winfarm");//当前用户的有权限的的风场

    this.currentFarm = this.farms[0];
    //--------------
    this.farms[0].isChecked = true;
    this.currentFarms.push(this.farms[0]);
    this.onSearch();
  }

  // 点击风场事件
  public onClickFarm(farm: Farm) {
    this.currentFarm = farm;
    this.currentFarms = [];
    this.currentFarms.push(this.currentFarm)
  }
  //指标点击事件
  public onClickTarget(e: any, target: any) {
    target.isChecked = !target.isChecked
    if (target.isChecked) {
      this.points.push(target.key);
      this.close();
    } else {
      this.points.splice(this.points.findIndex(item => item === target.key), 1)
    }
    this.points.sort();
  }
  //关闭弹出层事件
  close() {
    this.isShow = false;
  }
  //查询事件
  onSearch() {
    if (this.endtime > new Date()) {
      this.message = `结束时间不能大于当前时间！`;
      this.isShow = true;
    } else if (this.starttime > this.endtime) {
      this.message = `起始时间不能大于结束时间！`;
      this.isShow = true;
    } else if (this.points.length <= 0) {
      this.message = `指标不能为空！`;
      this.isShow = true;
    } else {
      this.currentFarmIds = []
      this.currentFarms.forEach(farm => {
        this.currentFarmIds.push(farm.farmId)
      })
      this.dynamicReportService.getDynamicReportByfarmID(this.currentFarmIds, this.points, this.starttime, this.endtime)
        .then(res => {
          if (res) {
            this.farmIndicators = [...res];
          };

          // this.farmIndicators = [
          //   {name:"aa",pointValueList:[{key:"ActivePower",value:1},{key:"WindSpeed",value:2}],type:1},
          //   {name:"aa",pointValueList:[{key:"ActivePower",value:3},{key:"WindSpeed",value:4}],type:1},
          //   {name:"aa",pointValueList:[{key:"ActivePower",value:5},{key:"WindSpeed",value:6}],type:2},
          // ]
          this.farmIndicators.forEach(item => {
            item.pointValueList.forEach(pointValue => {
              for (let key in pointValue) {
                item[pointValue.key] = pointValue.value
              }
            })
            if (item.type == 1) {
              item.name = "|—" + item.name;
            } else if (item.type == 2) {
              item.name = "|——" + item.name;
            } else if (item.type == 3) {
              item.name = "|———" + item.name;
            }
          })

        })
        .catch(error => console.log(error))
    }

  }

}



// WEBPACK FOOTER //
// ./src/app/module/dynamic-report/farm-indicator/farm-indicator.component.ts